home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / 1988 / 12 / portbl.asc < prev    next >
Text File  |  1988-12-31  |  4KB  |  141 lines

  1. _Building Software for Portability_
  2. by Greg Blackham
  3.  
  4. [EXAMPLE 1]
  5.  
  6.  
  7. /*    MACHINE.H                                      */
  8. /*    MACHINE DEPENDENT DATA TYPE DEFINITIONS           */
  9. /*    Copyright (C) 1987                   */
  10. /*    WordPerfect Corp.                              */
  11.  
  12. #ifdef    IBM-PC
  13.     typedef    unsigned char    BYTE;    /* unsigned,  8 bits */
  14.     typedef    int             WORD;    /* signed,   16 bits */
  15.     typedef    unsigned        UWORD;   /* unsigned, 16 bits */
  16.     typedef    long        DWORD;   /* signed,   32 bits */
  17.     typedef    unsigned long    UDWORD;     /* unsigned, 32 bits */
  18. #endif
  19.  
  20. #ifdef HP
  21.     typedef unsigned char   BYTE;     /* unsigned, 8 bits */
  22.     typedef short            WORD;     /* signed,  16 bits */
  23.     typedef unsigned short    UWORD;      /* unsigned, 16 bits */
  24.     typedef int             DWORD;    /* signed,   32 bits */
  25.     typedef unsigned int    UDWORD;   /* unsigned, 32 bits */
  26. #endif
  27.  
  28. #ifdef    ATT6386
  29.     typedef unsigned char    BYTE;     /* unsigned,  8 bits */
  30.     typedef short            WORD;     /* signed,   16 bits */
  31.     typedef unsigned short    UWORD;    /* unsigned, 16 bits */
  32.     typedef int             DWORD;    /* signed,   32 bits */
  33.     typedef unsigned int    UDWORD;   /* unsigned, 32 bits */
  34. #endif
  35.  
  36. . . .
  37.  
  38. [EXAMPLE 2]
  39.  
  40. /*******************************************************************
  41.  * This example program writes a data structure directly to disk.  *
  42.  * Only five bytes of data are significant, but more than 5 bytes  *
  43.  * are written to the file because of word alignment.  The         *
  44.  * component bytes of the integer data are also written in a       *
  45.  * different order depending on machine architecture.              *
  46.  *******************************************************************/
  47.  
  48. #include <fcntl.h>
  49. #include "machine.h"
  50.  
  51. /* 5 byte structure definition */
  52. /* word alignment may occur between elements 1 and 2! */
  53.  
  54. struct {
  55.     BYTE element1;
  56.     UDWORD element2;
  57. } s = {'A',             /* recognizable values for each byte */
  58.         0x08040201};    /* each byte represented by 2 hex digits */
  59.  
  60. main()
  61. {
  62.     int fd;
  63.     fd = open("data.fil",O_RDWR | O_CREAT,0666);
  64.     write(fd,(char *)&s,sizeof(s));   /* open and write file */
  65.     close(fd);
  66. }
  67.  
  68. Contents of data.fil created on HP 9000 Model 350:
  69.  
  70. 65,             This is the "A"
  71. 0,     This byte is inserted by the compiler for word alignment
  72. 8,     Bytes of element2 stored with the highest order byte first
  73. 4,
  74. 2,        
  75. 1               Lowest order byte of element2
  76.  
  77. Contents of data.fil created on AT&T 6386:
  78.  
  79. 65,             This is the "A"
  80. 0,     These 3 bytes are inserted by the compiler for word alignment
  81. 0,     The 6386 compiler aligns on 4-byte boundaries
  82. 0,
  83. 1,     Bytes of element2 stored with the highest order byte first
  84. 2,
  85. 4,
  86. 8              Highest order byte of element2
  87.  
  88.  
  89. [EXAMPLE 3]
  90.  
  91. /********************************************************************
  92.  * This new version of the example program writes the same data        *
  93.  * structure to disk in a portable way.  It creates a memory image  *
  94.  * of what the disk file should look like, then writes the file     *
  95.  * in one block.  When writing the integer data the bytes are       *
  96.  * written starting with the least significant byte.                *
  97.  *******************************************************************/
  98.  
  99. #include <fcntl.h>
  100. #include "machine.h"
  101.  
  102. /* 5 byte structure definition */
  103. /* word alignment may occur between elements 1 and 2! */
  104.  
  105. struct {
  106.     BYTE element1;
  107.     UDWORD element2;       /* 4 byte integer */
  108. } s = {'A',                    /* recognizable values for each byte */
  109.         0x08040201};   /* each byte represented by 2 hex digits */
  110.  
  111. main()
  112. {
  113.     int fd,               /* file descriptor */
  114.         i;            /* counter variable */
  115.     BYTE array[5];          /* output buffer */
  116.     UDWORD tmp;           /* a 4-byte integer */
  117.  
  118.     array[0] = s.element1;   /* 1-byte variables are easy */
  119.     tmp = s.element2;        /* Don't clobber the real data */
  120.     for (i=1; i<= 4; i++) {  /* put each byte of the UDWORD */
  121.         array[i] = (BYTE)(0xff & tmp);    /* in separately */
  122.         tmp >>= 8;       /* get next 8-bits */
  123.     }
  124.     
  125.     fd = open("data.fil",O_RDWR | O_CREAT,0666);    
  126.     write(fd,array,5);       /* open and write file */
  127.     close(fd);
  128. }
  129.  
  130. Contents of data.fil created on all machines using this code:
  131.  
  132. 65,          This is the "A"
  133. 1,     Bytes of element2 stored with the lowest order byte first
  134. 2,
  135. 4,        
  136. 8         Lowest order byte of element2
  137.  
  138.  
  139. [END OF BLACKHAM]
  140.  
  141.